package com.liesheng.haylou.ui.device;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.databinding.ActivityDeviceConcatenateBinding;
import com.liesheng.haylou.db.entity.BoundedDevice;
import com.liesheng.haylou.ui.device.vm.DeviceDetailVm;
import com.liesheng.haylou.ui.main.campaign.SportMapActivity;
import com.liesheng.haylou.ui.setting.SettingActivity;
import com.liesheng.haylou.ui.setting.SetttingEqActivity;
import com.liesheng.haylou.utils.ToastUtil;
import com.liesheng.haylou.view.dialog.DeviceDialog;


/**
 * Date:${DATA}
 * Author ${ZhangZhiQiang}
 * Description
 * 设备详情
 */
public class DeviceDetailActivity extends BaseActivity<ActivityDeviceConcatenateBinding, DeviceDetailVm> {

    BoundedDevice boundedDevice;
    BluetoothDevice bluetoothDevice;

    @Override
    protected ActivityDeviceConcatenateBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_device_concatenate, null, false);
        return mBinding;
    }


    @Override
    protected DeviceDetailVm getViewModel() {
        return new DeviceDetailVm(this);
    }

    @Override
    protected void initData() {
        showHeadLayout(true);
        setTitle("蓝牙耳机");
        setStatusBarColor(R.color.main_bg, false);
        setHeadRight("", R.drawable.device_btn_setting);

        mBinding.barBox.setEnabled(false);
        mBinding.barLeft.setEnabled(false);
        mBinding.barRight.setEnabled(false);

        boundedDevice = (BoundedDevice) getIntent().getSerializableExtra("BoundedDevice");
        bluetoothDevice = getIntent().getParcelableExtra("BluetoothDevice");

        mVm.init();

        mBinding.tvDeviceName.setText(boundedDevice.getName());
        if (boundedDevice.getLocations() != null && boundedDevice.getLocations().size() > 0) {
            mBinding.tvLocation.setText(boundedDevice.getLocations().get(0));
        } else {
            mBinding.tvLocation.setText("");
        }

        //连接蓝牙
        mVm.connentDevice();
    }

    @Override
    protected void openBluetooth(boolean isOpen) {
        super.openBluetooth(isOpen);
        if (isOpen)
            mVm.connentDevice();
    }

    @Override
    public void onClickView(View view) {

        if (!mVm.isConnect() && view.getId() != R.id.btUnlink && view.getId() != R.id.rlMap)
            return;

        switch (view.getId()) {
            case R.id.rlDeviceName://修改蓝牙名称
                showDeviceName();
                break;
            case R.id.rlMap://跳转地图导航
            case R.id.tvLocation://跳转地图导航
                SportMapActivity.startBy(this, boundedDevice.getLocations().get(0), boundedDevice.getLatitude(), boundedDevice.getLongitude());
                break;
            case R.id.tv_double_left://左耳机双击
            case R.id.rlHeadsetLeft://左耳机双击
                mVm.showContralDialog(0, mBinding.tvDoubleLeft.getText().toString());
                break;
            case R.id.tv_double_right://右耳机双击
            case R.id.rlHeadsetRight://右耳机双击
                mVm.showContralDialog(1, mBinding.tvDoubleRight.getText().toString());
                break;
            case R.id.tv_three_key://三击
            case R.id.layout_three_click://三击
                mVm.showContralDialog(2, mBinding.tvThreeKey.getText().toString());
                break;
            case R.id.btUnlink://断开连接
                mVm.changeConnect();
                break;
            case R.id.tv_eq://
                SetttingEqActivity.startBy(this, boundedDevice.getAddress());
                break;

        }
    }

    @Override
    protected void onClickRight() {
        super.onClickRight();
        SettingActivity.startBy(this, boundedDevice.getAddress(), mVm.getVersion());
    }


    private void showDeviceName() {
        final DeviceDialog deviceDialog = new DeviceDialog();
        deviceDialog.setOnDialogClickListener(new DeviceDialog.OnDialogClickListener() {
            @Override
            public void toPage() {
                ToastUtil.showToast("已保存");
            }

            @Override
            public void close() {
                deviceDialog.dismiss();
            }

            @Override
            public void save() {

            }
        });
        deviceDialog.show(DeviceDetailActivity.this);
    }


    public static void startBy(BaseFunActivity activity, BoundedDevice bd, BluetoothDevice device) {
        Intent intent = new Intent(activity, DeviceDetailActivity.class);
        intent.putExtra("BoundedDevice", bd);
        intent.putExtra("BluetoothDevice", device);
        activity.startActivity(intent);
    }

    public BoundedDevice getBoundedDevice() {
        return boundedDevice;
    }

    public BluetoothDevice getBluetoothDevice() {
        return bluetoothDevice;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mVm.notifiAdapter();
    }
}
